package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ITDeclarationMatchingService;
import com.mdd.admin.validate.TDeclarationMatchingCreateValidate;
import com.mdd.admin.validate.TDeclarationMatchingUpdateValidate;
import com.mdd.admin.validate.TDeclarationMatchingSearchValidate;
import com.mdd.admin.vo.TDeclarationMatchingListedVo;
import com.mdd.admin.vo.TDeclarationMatchingDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.TDeclarationMatching;
import com.mdd.common.mapper.TDeclarationMatchingMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 临时存项目政策的申报条件，公司哪些匹配或不匹配实现类
 * @author LikeAdmin
 */
@Service
public class TDeclarationMatchingServiceImpl implements ITDeclarationMatchingService {
        
    @Resource
    TDeclarationMatchingMapper tDeclarationMatchingMapper;

    /**
     * 临时存项目政策的申报条件，公司哪些匹配或不匹配列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TDeclarationMatchingListedVo>
     */
    @Override
    public PageResult<TDeclarationMatchingListedVo> list(PageValidate pageValidate, TDeclarationMatchingSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<TDeclarationMatching> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tDeclarationMatchingMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:projectId@project_id:int",
            "=:declaration:str",
            "=:customerId@customer_id:int",
            "=:isMatching@is_matching:int",
        });

        IPage<TDeclarationMatching> iPage = tDeclarationMatchingMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TDeclarationMatchingListedVo> list = new LinkedList<>();
        for(TDeclarationMatching item : iPage.getRecords()) {
            TDeclarationMatchingListedVo vo = new TDeclarationMatchingListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 临时存项目政策的申报条件，公司哪些匹配或不匹配详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TDeclarationMatching
     */
    @Override
    public TDeclarationMatchingDetailVo detail(Integer id) {
        TDeclarationMatching model = tDeclarationMatchingMapper.selectOne(
                new QueryWrapper<TDeclarationMatching>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TDeclarationMatchingDetailVo vo = new TDeclarationMatchingDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 临时存项目政策的申报条件，公司哪些匹配或不匹配新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TDeclarationMatchingCreateValidate createValidate) {
        TDeclarationMatching model = new TDeclarationMatching();
        model.setProjectId(createValidate.getProjectId());
        model.setDeclaration(createValidate.getDeclaration());
        model.setCustomerId(createValidate.getCustomerId());
        model.setIsMatching(createValidate.getIsMatching());
        tDeclarationMatchingMapper.insert(model);
    }

    /**
     * 临时存项目政策的申报条件，公司哪些匹配或不匹配编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TDeclarationMatchingUpdateValidate updateValidate) {
        TDeclarationMatching model = tDeclarationMatchingMapper.selectOne(
                new QueryWrapper<TDeclarationMatching>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setProjectId(updateValidate.getProjectId());
        model.setDeclaration(updateValidate.getDeclaration());
        model.setCustomerId(updateValidate.getCustomerId());
        model.setIsMatching(updateValidate.getIsMatching());
        tDeclarationMatchingMapper.updateById(model);
    }

    /**
     * 临时存项目政策的申报条件，公司哪些匹配或不匹配删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TDeclarationMatching model = tDeclarationMatchingMapper.selectOne(
                new QueryWrapper<TDeclarationMatching>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tDeclarationMatchingMapper.delete(new QueryWrapper<TDeclarationMatching>().eq("id", id));
    }

}
